var surveyNameLength = 24 * 2;
var surveyDescriptionLength = 40 * 2;
var surveyWordLength = 150 * 2;
var RS_SAMPLE_COUNT = 999999;
function initSurveyInfo(a) {
    survey = new Survey(a);
    if (survey.starttime != null) {
        survey.starttime = convertDate(survey.starttime)
    }
    if (survey.endtime != null) {
        survey.endtime = convertDate(survey.endtime)
    }
    $("#fname").html(a.name);
    $("#fdescription").html(a.description);
    $("#formName").val(a.name);
    showDescriptionEditor();
    $("#formWord").val(a.word);
    if ($.trim($("#formWord").val()) == "") {
        $("#formWord").val(ENDWORD);
        $("#formWord").addClass("gray9")
    }
    if (roleCode != "ts-enterprise") {
        $("#formWordtype").attr("disabled", "disabled");
        $("#UrlCheckDiv").after('<div style="top:0px; height:17px; width:80px;" class="cover-layer"><img width="78" height="17" onclick="showWordTypePop();return false;" src="' + imagePath + 'space.gif"></div>');
        $("#UrlDiv").after('<div style="top:20px; height:20px;" class="cover-layer"><img width="300" height="20" onclick="showWordTypePop();return false;" src="' + imagePath + 'space.gif"></div>');
        $("#formUrl").addClass("gray9");
        $("#formUrl").val(ENDWORD_URL)
    } else {
        if (a.wordtype == 1) {
            $("#formUrl").addClass("gray9")
        } else {
            a.wordtype = 2;
            $("#formWordtype").attr("checked", true);
            $("#formUrl").val(a.url);
            if ($.trim($("#formUrl").val()) == "") {
                $("#formUrl").val(ENDWORD_URL);
                $("#formUrl").addClass("gray9")
            }
        }
    }
    if (a.allowreport == 1) {
        $("#formAllowreport").attr("checked", true)
    } else {
        $("#formAllowreport").attr("checked", false)
    }
    if (a.samplecount == 0) {
        $("#formSamplecountselect").attr("checked", false);
        $("#formSamplecount").val("");
        $("#formSamplecount").attr("disabled", "disabled")
    } else {
        $("#formSamplecountselect").attr("checked", true);
        $("#formSamplecount").val(a.samplecount)
    }
   // $("#formSamplecounttext").html((auth.f_rs_sample_count > 0 ? ("(请输入数字1-" + auth.f_rs_sample_count + ")") : "请输入数字"));
    if (a.progressbar == 1) {
        $("#formProgress").attr("checked", true);
        $("#fprogressbar").show()
    } else {
        $("#formProgress").attr("checked", false)
    }
    if (a.isopen == 1) {
        $("#formIsOpen").attr("checked", true);
        $("#formIsOpenMsg").show()
    } else {
        $("#formIsOpenMsg").hide()
    }
    if (a.emailweek == 1) {
        $("#formEmailweek").attr("checked", true)
    } else {
        $("#formEmailweek").attr("checked", false)
    }
    if (a.emailmonth == 1) {
        $("#formEmailmonth").attr("checked", true)
    } else {
        $("#formEmailmonth").attr("checked", false)
    }
    if (a.emailfinish == 1) {
        $("#formEmailfinish").attr("checked", true)
    } else {
        $("#formEmailfinish").attr("checked", false)
    }
    $("#formEmail").val(a.email);
    if (a.emailnotice == 1) {
        $("#formEmailnotice").attr("checked", true)
    } else {
        $("#formEmailnotice").attr("checked", false);
        $("#formEmailweek").attr("disabled", "disabled");
        $("#formEmailmonth").attr("disabled", "disabled");
        $("#formEmailfinish").attr("disabled", "disabled");
        $("#formEmail").attr("disabled", "disabled")
    }
    if (a.accelerate == 1) {
        $("#formAccelerate").attr("checked", true)
    } else {
        $("#formAccelerate").attr("checked", false)
    }
    /*if (!checkAuth("f_notice_sms")) {
        $("#formMobileError").show()
    } else {
        $("#formMobile").val(a.mobile)
    }
    if (checkAuth("f_notice_sms")) {
        if (a.mobilenotice == 1) {
            $("#formMobilenotice").attr("checked", true)
        } else {
            $("#formMobilenotice").attr("checked", false);
            $("#formMobile").attr("disabled", "disabled")
        }
    } else {
        $("#formMobilenotice").attr("disabled", "disabled");
        $("#formMobile").attr("disabled", "disabled");
        $("#formMobileError").show();
        $("#mobileli").append('<div class="cover-layer"><img onclick="showMobilePop();return false;" width="300" height="30" src="' + imagePath + 'space.gif"></div>');
        $("#mobileli").addClass("ipo-c")
    }
    if (a.logo != null && a.logo != "") {
        $("#showlogo").html('<a onclick="changeSurveyLogo(); return false;" href="javascript:void(0);"><img  class="img120" src="' + (a.logo.indexOf("http://") < 0 ? (ctx + "/" + a.logo) : a.logo) + '"></a>');
        $("#showlogo").removeClass("logo-add")
    } else {
        $("#showlogo").html('<a onclick="changeSurveyLogo(); return false;" href="javascript:void(0);"><img width="16" height="16" src="' + imagePath + 'add.png">添加企业LOGO</a>')
    }*/
    if (a.allowcookies == 1) {
        $("#formAllowcookies").attr("checked", true)
    } else {
        $("#formAllowcookies").attr("checked", false)
    }
    if (a.allowip == 1) {
        $("#formAllowip").attr("checked", true)
    } else {
        $("#formAllowip").attr("checked", false)
    }
    if (a.timeschedule == 1) {
        $("#formTimeschedule").attr("checked", true);
        $("#listStartDate").show();
        if (survey.starttime != null) {
            $("#formStarttime").val(convertDate(survey.starttime))
        }
        $("#listEndDate").show();
        if (survey.endtime != null) {
            $("#formEndtime").val(convertDate(survey.endtime))
        }
    } else {
        $("#formTimeschedule").attr("checked", false);
        $("#listStartDate").hide();
        $("#listEndDate").hide()
    }
}
function showDescriptionEditor() {
    if (description_UEditor != null) {
        description_UEditor.destroy()
    }
    $("#formDescriptionDIV").html('<script type="text/plain" id="formDescription">' + survey.description + "<\/script>");
    description_UEditor = new UE.ui.Editor({
        initialContent: "",
        UEDITOR_HOME_URL: ctx + "/js/ueditor/",
        minFrameHeight: 60,
        zIndex: 1,
        toolbars: [["bold", "forecolor", "justifyleft", "justifycenter", "fontsize", "fontfamily", "removeformat"]],
        contextMenu: [],
        wordCount: false,
        elementPathEnabled: false
    });
    description_UEditor.render("formDescription");
    description_UEditor.ready(function() {
        description_UEditor.addListener("selectionChange", 
        function() {
            changeSurveyDescription()
        });
        description_UEditor.addListener("contentChange", 
        function() {
            changeSurveyDescription()
        });
        description_UEditor.addListener("keyup", 
        function(b, a) {
            var c = a.keyCode || a.which;
            if (c == 88) {
                changeSurveyDescription()
            }
        });
        description_UEditor.focus()
    })
}
function changeSurveyName() {
    editFlag = true;
    if (checkEnter($("#formName").val())) {
        $("#formName").val(replaceEnter($("#formName").val()))
    }
    survey.name = $.trim($("#formName").val());
    var a = getStringLength(survey.name);
    if (a > surveyNameLength) {
        $("#topName").html(getStringByByte(survey.name, surveyNameLength) + "...")
    } else {
        $("#topName").html(survey.name)
    }
    $("#topName").attr("title", survey.name);
    $("#fname").html(survey.name)
}
function changeSurveyDescription() {
    editFlag = true;
    survey.descriptiontext = replaceQuestion(description_UEditor.getPlainTxt());
    survey.description = replaceQuestion(description_UEditor.getContent());
    $("#fdescription").html(survey.description)
}
function changeSurveyWordtype(a) {
    editFlag = true;
    if ($("#formWordtype").is(":checked")) {
        survey.wordtype = 2
    } else {
        survey.wordtype = 1
    }
    if (survey.wordtype == 2) {
        $("#formWordUrlError").hide();
        $("#formWordUrlNull").hide();
        $("#formUrl").val("");
        $("#formUrl").removeClass("gray9")
    } else {
        $("#formUrl").val(ENDWORD_URL);
        $("#formUrl").addClass("gray9");
        survey.url = "";
        wordurlcheck = false
    }
}
var wordurlcheck = true;
function changeSurveyWord() {
    editFlag = true;
    if (checkEnter($("#formWord").val())) {
        $("#formWord").val(replaceEnter($("#formWord").val()))
    }
    var b = getStringLength($("#formWord").val());
    if (b > surveyWordLength) {
        var a = getStringByByte($("#formWord").val(), surveyWordLength);
        $("#formWord").val(a);
        survey.word = a
    } else {
        survey.word = $("#formWord").val()
    }
}
function changeSurveyUrl() {
    editFlag = true;
    survey.url = $("#formUrl").val();
    wordurlcheck = false;
    $("#formWordtype").attr("checked", true)
}

// 校验URL
function checkWordUrl() {
    $("#formWordUrlError").hide(); // 隐藏校验URL提示
    $("#formWordUrlNull").hide(); // 隐藏校验URL提示
    if (!$("#stage").hasClass("cfo")) { // 已经切换了不再进行
        return true
    }
    if (survey.wordtype != 2) { // 调查问卷的类型 ？？？
        return true
    }
    if (!wordurlcheck) { // URL 校验不通过
        if ($.trim(survey.url) == "") { // 验证URL
            $("#formWordUrlError").hide();
            $("#formWordUrlNull").show();
            return false
        } else {
            $("#formWordUrlNull").hide();
            if ($.trim(survey.url).indexOf("http://") < 0 && $.trim(survey.url).indexOf("https://") < 0) {
                survey.url = "http://" + $.trim(survey.url)
            }
            if (!checkeURL(survey.url)) {
                $("#formWordUrlError").hide();
                $("#formWordUrlNull").show();
                return false
            } else {
                $("#formUrl").val(survey.url);
                setWordUrlCheck();
                return true
            }
        }
    } else {
        return true
    }
}
function setWordUrlCheck() {
    wordurlcheck = true;
    $("#formWordUrlNull").hide();
    $("#formWordUrlError").hide()
}
function changeSurveyAllowreport() {
    editFlag = true;
    if ($("#formAllowreport").attr("checked")) {
        survey.allowreport = 1
    } else {
        survey.allowreport = 0
    }
}
function setSurveySamplecount() {
    editFlag = true;
    if ($("#formSamplecountselect").attr("checked")) {
        $("#formSamplecount").attr("disabled", false)
    } else {
        $("#formSamplecount").attr("disabled", "disabled");
        $("#formSamplecount").val("");
        survey.samplecount = 0
    }
}
function changeSurveySamplecount() {
    var a = survey.samplecount;
    editFlag = true;
    if ($("#formSamplecount").val() == "") {
        survey.samplecount = 0
    } else {
        if (!checkNum($.trim($("#formSamplecount").val()))) {
            survey.samplecount = 0;
            $("#formSamplecount").val("")
        } else {
            var b = parseInt($("#formSamplecount").val(), 10);
            if (b <= 0) {
                survey.samplecount = 0;
                $("#formSamplecount").val("")
            } else {
                if (auth.f_rs_sample_count > 0 && auth.f_rs_sample_count < b) {
                    $("#formSamplecount").val(auth.f_rs_sample_count);
                    $("#poptips").html(SamplecountInfo());
                    showPopTips()
                } else {}
                survey.samplecount = parseInt($("#formSamplecount").val(), 10)
            }
        }
    }
}
function changeSurveyProgress() {
    editFlag = true;
    if ($("#formProgress").attr("checked")) {
        survey.progressbar = 1;
        $("#fprogressbar").show()
    } else {
        survey.progressbar = 0;
        $("#fprogressbar").hide()
    }
}
function changeSurveyIsOpen() {
    editFlag = true;
    if ($("#formIsOpen").attr("checked")) {
        survey.isopen = 1;
        $("#formIsOpenMsg").show()
    } else {
        survey.isopen = 0;
        $("#formIsOpenMsg").hide()
    }
}
function changeSurveyType() {
    editFlag = true;
    survey.type = parseInt($("#surveyCatalogSelect").val())
}
function changeSurveyTheme() {
    editFlag = true;
    survey.themeid = $("#formTheme").val()
}
function changeSurveyEmailnotice() {
    editFlag = true;
    if ($("#formEmailnotice").attr("checked")) {
        survey.emailnotice = 1;
        survey.emailfinish = 1;
        $("#formEmail").attr("disabled", false);
        $("#formEmailweek").attr("disabled", false);
        $("#formEmailmonth").attr("disabled", false);
        $("#formEmailfinish").attr("disabled", false);
        $("#formEmailfinish").attr("checked", true)
    } else {
        survey.emailnotice = 0;
        survey.emailweek = 0;
        survey.emailmonth = 0;
        survey.emailfinish = 0;
        $("#formEmailweek").attr("checked", false);
        $("#formEmailmonth").attr("checked", false);
        $("#formEmailfinish").attr("checked", false);
        $("#formEmail").attr("disabled", "disabled");
        $("#formEmailweek").attr("disabled", "disabled");
        $("#formEmailmonth").attr("disabled", "disabled");
        $("#formEmailfinish").attr("disabled", "disabled")
    }
}
function changeSurveyEmailweek() {
    editFlag = true;
    if ($("#formEmailweek").attr("checked")) {
        survey.emailweek = 1
    } else {
        survey.emailweek = 0
    }
    checkSurveyEmailnotice()
}
function changeSurveyEmailmonth() {
    editFlag = true;
    if ($("#formEmailmonth").attr("checked")) {
        survey.emailmonth = 1
    } else {
        survey.emailmonth = 0
    }
    checkSurveyEmailnotice()
}
function changeSurveyEmailfinish() {
    editFlag = true;
    if ($("#formEmailfinish").attr("checked")) {
        survey.emailfinish = 1
    } else {
        survey.emailfinish = 0
    }
    checkSurveyEmailnotice()
}
function checkSurveyEmailnotice() {
    if ($("#formEmailweek").attr("checked") || $("#formEmailmonth").attr("checked") || $("#formEmailfinish").attr("checked")) {} else {
        $("#formEmailnotice").attr("checked", false);
        survey.emailnotice = 0;
        changeSurveyEmailnotice()
    }
}
function changeSurveyMobilenotice() {
    if (checkAuth("f_notice_sms")) {
        editFlag = true;
        if ($("#formMobilenotice").attr("checked")) {
            survey.mobilenotice = 1;
            $("#formMobile").attr("disabled", false)
        } else {
            survey.mobilenotice = 0;
            $("#formMobile").attr("disabled", "disabled")
        }
    }
}
function changeSurveyMobile() {
    editFlag = true;
    survey.mobile = $("#formMobile").val()
}
function changeSurveyEmail() {
    editFlag = true;
    survey.email = $("#formEmail").val()
}
function changeSurveyAccelerate() {
    var a = "";
    if ($("#formAccelerate").attr("checked")) {
        a = accmap.open
    } else {
        a = accmap.close
    }
    artconfirmNoStyle(a, 
    function() {
        editFlag = true;
        if ($("#formAccelerate").attr("checked")) {
            survey.accelerate = 1
        } else {
            survey.accelerate = 0
        }
    },
    function() {
        if ($("#formAccelerate").attr("checked")) {
            $("#formAccelerate").attr("checked", false)
        } else {
            $("#formAccelerate").attr("checked", true)
        }
    })
}
function changeSurveyLogo() {
    if (!checkAuth("f_custom_logo")) {
        showLogoPop();
        return
    }
    if (survey.logo != "") {
        if (survey.logo.indexOf("http://") < 0) {
            showLogoEditDiv("1");
            $("#logopreviewdiv").removeClass("edit-pic");
            $("#logopreview").css("width", 120).css("height", "auto").show();
            $("#logopreview").html('<img id="logopreviewimg" class="img120" src="' + ctx + "/" + survey.logo + '">');
            $("#logodelete").remove();
            $("#logopreview").after('<div id="logodelete" class="upic-del"><a class="blue" href="javascript:void(0);" onclick="removeSurveyLogo();return false;">删除</a></div>');
            moveLogoDiv(1)
        } else {
            showLogoEditDiv("2");
            $("#logoremoteurl").val(survey.logo);
            $("#logoremoteimg").attr("src", survey.logo);
            $("#logoremote1").hide();
            $("#logoremote2").show()
        }
    } else {
        showLogoEditDiv("1");
        $("#logopreviewdiv").removeClass("edit-pic");
        $("#logopreview").html('<div class="initial">企业logo</div>').css("width", 120).css("height", 40).show();
        moveLogoDiv(1)
    }
    showFloatLayer("#logoEditDIV")
}
function changeSurveycookies() {
    editFlag = true;
    if ($("#formAllowcookies").attr("checked")) {
        survey.allowcookies = 1
    } else {
        survey.allowcookies = 0
    }
}
function changeSurveyip() {
    editFlag = true;
    if ($("#formAllowip").attr("checked")) {
        survey.allowip = 1
    } else {
        survey.allowip = 0
    }
}
function showTimeschedule() {
    editFlag = true;
    if ($("#formTimeschedule").attr("checked")) {
        survey.timeschedule = 1;
        $("#listStartDate").show();
        $("#listEndDate").show()
    } else {
        survey.timeschedule = 0;
        $("#listStartDate").hide();
        $("#listEndDate").hide()
    }
};